package top.aprillie.mamabike.user.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.aprillie.mamabike.common.constants.Constants;
import top.aprillie.mamabike.common.exception.MaMaBikeException;
import top.aprillie.mamabike.common.resp.ApiResult;
import top.aprillie.mamabike.common.rest.BaseController;
import top.aprillie.mamabike.user.entity.LoginInfo;
import top.aprillie.mamabike.user.entity.User;
import top.aprillie.mamabike.user.entity.UserElement;
import top.aprillie.mamabike.user.service.UserService;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: illure
 * @Description:
 * @Date: Created in 21:13 2018/2/3
 * @Modified By:
 */
@RestController
@RequestMapping("user")
@Slf4j
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     * @Author: illure
     * @Date: 9:38 2018/2/18
     * @Description: 用户登录
     * @Params: [loginInfo]
     * @Return: top.aprillie.mamabike.common.resp.ApiResult<java.lang.String>
     */
    @ApiOperation(value = "用户登陆", notes = "用户登陆", httpMethod = "POST")
    @ApiImplicitParam(name = "loginInfo", value = "加密数据", required = true, dataType = "LoginInfo")
    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResult<String> login(@RequestBody LoginInfo loginInfo) {
        ApiResult<String> resp = new ApiResult<>();
        try {
            String data = loginInfo.getData();
            String key = loginInfo.getKey();
            if (StringUtils.isBlank(data) || StringUtils.isBlank(key)) {
                throw new MaMaBikeException("参数校验失败");
            }
            String token = userService.login(data, key);
            resp.setData(token);
        } catch (MaMaBikeException e) {
            resp.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            resp.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("Fail to login!", e);
            resp.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            resp.setMessage("内部错误");
        }
        return resp;
    }

    /**
     * @Author: illure
     * @Date: 9:38 2018/2/18
     * @Description: 修改用户昵称
     * @Params: [user]
     * @Return: top.aprillie.mamabike.common.resp.ApiResult
     */
    @ApiOperation(value = "修改昵称", notes = "用户修改昵称", httpMethod = "POST")
    @ApiImplicitParam(name = "user", value = "用户信息 包含昵称", required = true, dataType = "User")
    @RequestMapping("/modifyNickname")
    public ApiResult modifyNickname(@RequestBody User user) {
        ApiResult resp = new ApiResult();
        try {
            UserElement ue = getCurrentUser();
            user.setId(ue.getUserId());
            userService.modifyNickname(user);
        } catch (MaMaBikeException e) {
            resp.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            resp.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("Fail to login!", e);
            resp.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            resp.setMessage("内部错误");
        }
        return resp;
    }

    /**
     * @Author: illure
     * @Date: 9:37 2018/2/18
     * @Description: 短信验证码
     * @Params: [user, request]
     * @Return: top.aprillie.mamabike.common.resp.ApiResult
     */
    @ApiOperation(value = "短信验证码", notes = "根据用户手机号码发送验证码", httpMethod = "POST")
    @ApiImplicitParam(name = "user", value = "用户信息 包含手机号码", required = true, dataType = "User")
    @RequestMapping("/sendVercode")
    public ApiResult sendVercode(@RequestBody User user, HttpServletRequest request) {
        ApiResult resp = new ApiResult();
        try {
            userService.sendVercode(user.getMobile(), getIpFromRequest(request));
        } catch (MaMaBikeException e) {
            resp.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            resp.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("Fail to send sms vercode!", e);
            resp.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            resp.setMessage("内部错误");
        }
        return resp;
    }

    /**
     * @Author: illure
     * @Date: 9:37 2018/2/18
     * @Description: 上传用户头像
     * @Params: [req, file]
     * @Return: top.aprillie.mamabike.common.resp.ApiResult<java.lang.String>
     */
    @ApiOperation(value = "上传头像", notes = "用户上传头像 file", httpMethod = "POST")
    @RequestMapping(value = "/uploadHeadImg", method = RequestMethod.POST)
    public ApiResult<String> uploadHeadImg(HttpServletRequest req, @RequestParam(required = false) MultipartFile file) {
        ApiResult<String> resp = new ApiResult<>();
        try {
            UserElement ue = getCurrentUser();
            userService.uploadHeadImg(file, ue.getUserId());
            resp.setMessage("上传成功");
        } catch (MaMaBikeException e) {
            resp.setCode(e.getStatusCode());
            resp.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("Fail to update user info", e);
            resp.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            resp.setMessage("内部错误");
        }
        return resp;
    }
}
